<?php


namespace app\login\controller;


use app\api\service\Token;
use app\login\model\Account;
use app\login\model\SignUpdate;
use app\login\service\FindSignService;
use app\login\service\SignService;
use app\login\validate\PageValidate;
use think\facade\Request;
use app\login\model\Sign as SignModel;

class Sign extends BaseController
{
    # 渲染添加页面
    public function addSign()
    {
        $info = [
            'sign_id' => '',
            'pwd' => '',
            'status' => '',
            'host_d' => '',
            'host_f' => '',
        ];
        $this->assign('info', $info);
        return $this->fetch('sign/addSign');
    }
    # 渲染编辑渠道页面
    public function updateSign($id, $token)
    {
        $name = Token::getRedisTokenNameNotInHerder($token);
        $model = new SignUpdate();
        $info = $model->findOne($id);
        $this->assign('info', $info);
        return $this->fetch('sign/addSign');
    }
    # 渲染所有渠道列表页面
    public function allSign()
    {
        return $this->fetch('sign/allSign');
    }

    /**
     * 添加或者更新sign
     * @return \think\response\Json
     */
    public function signApi()
    {
        $name = Token::getRedisTokenName();
        $data = Request::post('data');
        $service = new SignService($name);
        $result = $service->addOrUpdateSign($data);
        return json($result);
    }

    /**
     * 分页
     * @param $page 第几页
     * @param $limit 每一个数量
     * @return \think\response\Json
     */
    public function allSignApi($page, $limit)
    {
        (new PageValidate()) ->goCheck();
        $name = Token::getRedisTokenName();
        $service = new SignService($name);
        $result = $service->findAll($page, $limit);
        return json($result);
    }

    /**
     * 根据id删除对应的推广账号
     * @param $id
     * @return \think\response\Json
     */
    public function delSignApi($id)
    {
        $name = Token::getRedisTokenName();
        $service = new SignService($name);
        $result = $service->delOneSign($id);
        return json($result);
    }

    /**
     * 按条件查找对应的渠道号
     * @return \think\response\Json
     */
    public function findSignApi()
    {
        $name = Token::getRedisTokenName();
        $post = Request::get();
        $service = new FindSignService();
        $result = $service->getSignByCondition($name,$post);
        return json($result);
    }
}